﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FacebookWrapper;
using FacebookWrapper.ObjectModel;

namespace B13_Ex01_David_300512340_Alexey_321203689
{
    public partial class MainForm : Form
    {
        private MainLogic m_FacebookOperator = new MainLogic();
        private AlbumWrapper m_SelectedAlbum;
        private string m_SelectedFriendName = string.Empty;

        public MainForm()
        {
            InitializeComponent();
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            loginToFacebook();
        }

        private void loginToFacebook()
        {
            m_FacebookOperator.Login();
            if (m_FacebookOperator.LoggedInUser != null)
            {
                fetchUserInfo();
            }
        }

        private void fetchUserInfo()
        {
            if (m_FacebookOperator.IsLogedIn)
            {
                labelName.Text = m_FacebookOperator.LoggedInUser.ToString();
                pictureBoxProfile.Load(m_FacebookOperator.LoggedInUser.ProfilePicture);
                listBoxFriends.DataSource = m_FacebookOperator.LoggedInUser.GetFriendsList();
                listBoxAlbums.DataSource = m_FacebookOperator.LoggedInUser.GetAlbumsList();
                listBoxEvents.DisplayMember = "Name";
                listBoxEvents.DataSource = m_FacebookOperator.LoggedInUser.GetEventsList();
            }
        }

        private void listBoxFriends_SelectedIndexChanged(object sender, EventArgs e)
        {
            displaySelectedFriend();
        }

        private void displaySelectedFriend()
        {
            if (listBoxFriends.SelectedItems.Count == 1)
            {
                UserWrapper selectedFriend = listBoxFriends.SelectedItem as UserWrapper;

                if (selectedFriend.UserInformation.PictureNormalURL != null)
                {
                    m_SelectedFriendName = selectedFriend.ToString();
                    pictureBoxFriend.LoadAsync(selectedFriend.ProfilePicture);
                    listBoxAlbums.DataSource = selectedFriend.GetAlbumsList(); // To display each friend album list
                }
                else
                {
                    pictureBoxFriend.Image = pictureBoxFriend.ErrorImage;
                }
            }
        }

        private void listBoxEvents_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxEvents.SelectedItems.Count == 1)
            {
                Event selectedEvent = listBoxEvents.SelectedItem as Event;

                pictureBoxEvent.LoadAsync(selectedEvent.PictureNormalURL);
            }
        }

        private void linkLabelNewFeed_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (m_FacebookOperator.IsLogedIn)
            {
                listBoxFeed.DataSource = m_FacebookOperator.LoggedInUser.GetFeedsList();
            }
        }

        private void buttonPost_Click(object sender, EventArgs e)
        {
            if (m_FacebookOperator.IsLogedIn)
            {
                m_FacebookOperator.LoggedInUser.UserInformation.PostStatus(textBoxStatus.Text);
                MessageBox.Show(null, "Done posting status.", "Post status", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBoxStatus.Clear();
            }
        }

        private void listBoxAlbums_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxAlbums.SelectedItems.Count == 1)
            {
                m_SelectedAlbum = listBoxAlbums.SelectedItem as AlbumWrapper;
                try
                {
                    pictureBoxAlbumCover.LoadAsync(m_SelectedAlbum.GetAlbumCoverURL());
                }
                catch
                {
                    MessageBox.Show(
                        "No pictures in this album.",
                        "Sorry",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Exclamation,
                        MessageBoxDefaultButton.Button1);
                }
            }
        }

        private void buttonDisplayAlbum_Click(object sender, EventArgs e)
        {
            AlbumForm albumFormDialogBox = null;

            if (m_FacebookOperator.IsLogedIn)
            {
                albumFormDialogBox = new AlbumForm(m_SelectedAlbum);
                albumFormDialogBox.Text = m_SelectedFriendName + " - Album"; 
                albumFormDialogBox.ShowDialog();
            }
        }

        private void linkLabelMyAlbum_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            listBoxAlbums.DataSource = m_FacebookOperator.LoggedInUser.GetAlbumsList();
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            if (m_FacebookOperator.IsLogedIn && (checkBoxSearchPostByWord.Checked || checkBoxSearchPostByDate.Checked))
            {
                FeedWrapper feed = new FeedWrapper();

                if (checkBoxSearchPostByWord.Checked)
                {
                    listBoxFeed.DataSource = null;
                    listBoxFeed.Items.Clear();
                    listBoxFeed.DataSource = feed.SearchPostByWord(m_FacebookOperator.LoggedInUser, textBoxSearchPostByWord.Text);
                }
                else
                {
                    if (checkBoxSearchPostByDate.Checked)
                    {
                        listBoxFeed.DataSource = feed.SearchPostByDate(m_FacebookOperator.LoggedInUser, dateTimePickerFrom.Value, dateTimePickerTo.Value);
                    }
                    else
                    {
                        if (checkBoxSearchPostByWord.Checked && checkBoxSearchPostByDate.Checked)
                        {
                            listBoxFeed.DataSource = feed.SearchPostByWordAndDate(m_FacebookOperator.LoggedInUser, textBoxSearchPostByWord.Text, dateTimePickerFrom.Value, dateTimePickerTo.Value);
                        }
                    }
                }
            }
        }
    }
}